package com.exam.sacrifice.viewmodel;

import androidx.lifecycle.ViewModel;

import com.exam.commonbiz.net.BaseObserver;
import com.exam.commonbiz.net.BaseResponse;
import com.exam.commonbiz.net.ExceptionHandle;
import com.exam.commonbiz.net.XApi;
import com.exam.sacrifice.api.ApiService;
import com.exam.sacrifice.ui.bean.ActivityListReq;
import com.exam.sacrifice.ui.bean.ActivityListRes;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ActivityViewModel extends ViewModel {

    private IActivityCallBack activityCallBack;

    public void setActivityCallBack(IActivityCallBack activityCallBack) {
        this.activityCallBack = activityCallBack;
    }

    public void activityList(int pageIndex, String keyword) {

        ActivityListReq activityListReq = new ActivityListReq();
        activityListReq.keyword = keyword;
        activityListReq.pageSize = 10;
        activityListReq.pageIndex = pageIndex;

        XApi.get(ApiService.class)
                .activityList(activityListReq)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ActivityListRes>() {

                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onSuccess(BaseResponse<ActivityListRes> response) {
                        if (activityCallBack != null) {
                            activityCallBack.onActivityList(response.getData());
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable responeThrowable) {
                        if (activityCallBack != null) {
                            activityCallBack.onActivityList(null);
                        }
                    }

                    @Override
                    public void onFinish() {

                    }

                });
    }
}
